package com.aizhixin.lab.company.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.UserInforCore;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.NoticeDomain;
import com.aizhixin.lab.company.domain.SystemSetDomain;
import com.aizhixin.lab.company.service.NoticeService;
import com.aizhixin.lab.company.service.SystemSetService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/v1/base/notice")
@Api(value = "公告管理相关API")
public class NoticeController {
	@Autowired
	private AccountService accountService;
	@Autowired
	private NoticeService noticeService;

	
	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "保存公告信息", response = Void.class, notes = "保存公告信息<br>@author zhengning")
	public ResponseEntity<?> saveInfor(@RequestHeader("Authorization") String token,
			 @RequestBody NoticeDomain dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		
		Map<String, Object> result = noticeService.save(dto);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/changstatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "发布/删除公告", response = Void.class, notes = "发布/删除公告信息<br>@author zhengning")
	public ResponseEntity<?> saveInfor(@RequestHeader("Authorization") String token,
			@ApiParam(value = "公告id", required = false) @RequestParam(value = "id", required = true) Long id,
			@ApiParam(value = "修改状态，0（发布）1（删除） ", required = false) @RequestParam(value = "status", required = true) Integer status) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		
		Map<String, Object> result = noticeService.changeStatus(id, status);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/getpage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "分页查询公告信息", response = Void.class, notes = "分页查询公告信息<br>@author zhengning")
	public ResponseEntity<?> getInfor(@RequestHeader("Authorization") String token,
			 @RequestBody NoticeDomain dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = null;
        if(UserInforCore.ROLE_TEACHER.equals(account.getRole()) || UserInforCore.ROLE_STUDENT.equals(account.getRole()) ){
        	result = noticeService.getPage(PageJdbcUtil.createNoErrorPageRequest(dto.getPageNumber(), dto.getPageSize()), dto.getName(),account.getId());
        }else{
           result = noticeService.getPageForAdmin(PageJdbcUtil.createNoErrorPageRequest(dto.getPageNumber(), dto.getPageSize()), dto.getName());
        }
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/getone", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "根据id查询公告信息", response = Void.class, notes = "根据id查询公告信息<br>@author zhengning")
	public ResponseEntity<?> getOne(@RequestHeader("Authorization") String token,
			@ApiParam(value = "公告id", required = false) @RequestParam(value = "id", required = true) Long id) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
		Map<String, Object> result = noticeService.getOne(id);
		return new ResponseEntity(result, HttpStatus.OK);
	}


}
